package com.doge.walknovel.business.novel

import com.doge.walknovel.api.model.NovelDetailInfo
import com.doge.walknovel.utils.*
import java.io.*

object NovelDetailCacheHelper {
    private val novelsDir: File get() {
        return File(appContext.externalCacheDir, "novelDetails").apply {
            if (!exists()) mkdirs()
        }
    }

    fun saveCache(novelId: Int, data: NovelDetailInfo) = runOnIO {
        val file = File(novelsDir, novelId.toString())
        FileUtils.write(file, toJson(data))
    }

    fun loadCache(novelId: Int): String? {
        require(!isMainThread())
        val file = File(novelsDir, novelId.toString())
        return FileUtils.read(file)
    }
}